package com.geekcoding.geekoj.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.geekcoding.common.enums.State;
import com.geekcoding.geekoj.entity.Question;
import com.geekcoding.geekoj.entity.UserEntity;
import com.geekcoding.geekoj.entity.UserQuestionFavor;
import com.geekcoding.geekoj.mapper.UserQuestionFavorMapper;
import org.springframework.stereotype.Repository;

import java.util.Objects;

@Repository
 public class UserQuestionFavorRepository extends BaseRepository<UserQuestionFavor, UserQuestionFavor, UserQuestionFavorMapper>{

    @Override
    public UserQuestionFavor convert(UserQuestionFavor entity) {
        return entity;
    }

    public UserQuestionFavor nullOrCreate(Question question, UserEntity userEntity) {
        LambdaQueryWrapper<UserQuestionFavor> queryChainWrapper = Wrappers.<UserQuestionFavor>lambdaQuery().eq(UserQuestionFavor::getQuestionId, question.getId()).eq(UserQuestionFavor::getUserId, userEntity.getId());
        UserQuestionFavor userQuestionFavor = baseMapper.selectOne(queryChainWrapper);
        if (Objects.isNull(userQuestionFavor)) {
            userQuestionFavor = UserQuestionFavor.builder().userId(userEntity.getId()).questionId(question.getId()).state(State.NORMAL).build();
        }
        return userQuestionFavor;
    }
}
